package com.fwzc.giuseppe.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fwzc.giuseppe.MyApplication;
import com.fwzc.giuseppe.R;
import com.fwzc.giuseppe.Api.Api;
import com.fwzc.giuseppe.adapter.BannerAdapter;
import com.fwzc.giuseppe.adapter.BannerInfo;
import com.fwzc.giuseppe.adapter.HolderAdapter;
import com.fwzc.giuseppe.adapter.HolderAdapter.ViewCreator;
import com.fwzc.giuseppe.base.BaseActivity;
import com.fwzc.giuseppe.config.Config;
import com.fwzc.giuseppe.util.DialogUtils;
import com.fwzc.giuseppe.util.PhoneHelper;
import com.fwzc.giuseppe.view.BaseViewPager.OnPageChangeListener;
import com.fwzc.giuseppe.view.LoopPageIndicator;
import com.fwzc.giuseppe.view.LoopViewPager;
import com.fwzc.giuseppe.view.LoopViewPagerAdapter;
import com.lidroid.xutils.util.LogUtils;

public class HomepageActivity extends BaseActivity implements OnClickListener {
	public DrawerLayout drawerLayout;
	private LinearLayout left_menu_layout;
	private Context context = HomepageActivity.this;
	private Button Left, Right;
	private LoopViewPager mViewPager;
	private LoopViewPagerAdapter mPagerAdapter;
	private LoopPageIndicator mPagerIndicator;
	private GridView gv;
	private TextView exit;
	HolderAdapter<HashMap<String, String>> mAdapter_grid;
	private List<HashMap<String, String>> data_grid = new ArrayList<HashMap<String, String>>();
	RelativeLayout myself, favorites, not_receiving, had_receiving, all_orders,
			invitation, setting, message;
	int width = 0;
	MyApplication myApp;
	/** 再按一次退出程序 */
	private long exitTime = 0;

	@SuppressWarnings("static-access")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.homepage);
		myApp = (MyApplication) context.getApplicationContext();
		mPagerIndicator = (LoopPageIndicator) findViewById(R.id.task_list_id_indicator);
		mViewPager = (LoopViewPager) findViewById(R.id.task_list_id_banner);
		width = PhoneHelper.getScreenWidth(this);
		myApp.initImageLoader(this);

		Left = (Button) findViewById(R.id.person_center);
		Left.setOnClickListener(this);
		Right = (Button) findViewById(R.id.shopping);
		Right.setOnClickListener(this);

		initLeftLayout();
		initView();
	}

	public void initLeftLayout() {

		drawerLayout = (DrawerLayout) findViewById(R.id.main_homepage);
		// 左边菜单

		left_menu_layout = (LinearLayout) findViewById(R.id.main_left_drawer_layout);

		View view = getLayoutInflater().inflate(R.layout.person_center, null);

		left_menu_layout.addView(view);

		exit = (TextView) findViewById(R.id.exit);
		exit.setOnClickListener(this);
	}

	private void initView() {
		myself = (RelativeLayout) findViewById(R.id.myself);
		myself.setOnClickListener(this);
		favorites = (RelativeLayout) findViewById(R.id.favorites);
		favorites.setOnClickListener(this);
		not_receiving = (RelativeLayout) findViewById(R.id.not_receiving);
		not_receiving.setOnClickListener(this);
		had_receiving = (RelativeLayout) findViewById(R.id.had_receiving);
		had_receiving.setOnClickListener(this);
		all_orders = (RelativeLayout) findViewById(R.id.all_orders);
		all_orders.setOnClickListener(this);
		invitation = (RelativeLayout) findViewById(R.id.invitation);
		invitation.setOnClickListener(this);
		setting = (RelativeLayout) findViewById(R.id.setting);
		setting.setOnClickListener(this);
		message = (RelativeLayout) findViewById(R.id.message);
		message.setOnClickListener(this);
		gv = (GridView) findViewById(R.id.gridview);
		gv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				LogUtils.d("----------------------------");
				Intent intent = new Intent(context, CommodityDetails.class);
				startActivity(intent);
			}
		});

		createAdapterGrid();
		if (Api.false_data) {
			setFalseData();
			setFalseDataGrad();
		} else {

			loadingHomePage();

		}
	}

	private void createAdapterGrid() {
		ViewCreator<HashMap<String, String>> creator = new ViewCreator<HashMap<String, String>>() {
			@SuppressLint("InflateParams")
			@Override
			public View createView(LayoutInflater inflater, int position,
					HashMap<String, String> map) {
				// TODO Auto-generated method stub

				return LayoutInflater.from(context).inflate(
						R.layout.item_grid_homepage, null);
			}

			@Override
			public void updateView(View view, int position,
					final HashMap<String, String> map) {
				// TODO Auto-generated method stub

				ImageView img_parents = (ImageView) view
						.findViewById(R.id.img_parents);
				TextView tv_parents = (TextView) view
						.findViewById(R.id.tv_name);
				TextView tv_sale = (TextView) view
						.findViewById(R.id.tv_salesum);
				TextView tv_price = (TextView) view.findViewById(R.id.tv_price);

				imageLoader.displayImage(map.get("imageUrl"), img_parents,
						Config.no_iv_option);// get图片
				tv_parents.setText(map.get("name"));// get文字
				tv_sale.setText(map.get("sale"));
				tv_price.setText(map.get("price"));
			}

		};

		mAdapter_grid = new HolderAdapter<HashMap<String, String>>(
				this.getLayoutInflater(), creator);
		gv.setAdapter(mAdapter_grid);
		mAdapter_grid.update(data_grid);

	}

	/**
	 * 伪数据
	 * */
	private void setFalseData() {
		ArrayList<BannerInfo> mInfos = new ArrayList<BannerInfo>();

		List<HashMap<String, String>> banner_list = new ArrayList<HashMap<String, String>>();

		HashMap<String, String> infomap = new HashMap<String, String>();

		// infomap.clear();
		infomap.put("mImgUrl",
				"http://pic31.nipic.com/20130718/12115012_113639688000_2.jpg");
		banner_list.add(infomap);

		// infomap.clear();
		HashMap<String, String> infomap1 = new HashMap<String, String>();
		infomap1.put("mImgUrl",
				"http://pic31.nipic.com/20130718/12115012_113639688000_2.jpg");
		banner_list.add(infomap1);

		// infomap.clear();
		HashMap<String, String> infomap2 = new HashMap<String, String>();
		infomap2.put("mImgUrl",
				"http://pic31.nipic.com/20130718/12115012_113639688000_2.jpg");
		banner_list.add(infomap2);

		for (int i = 0; i < banner_list.size(); i++) {
			BannerInfo info = new BannerInfo();

			info.mImgUrl = banner_list.get(i).get("mImgUrl");
			System.out.println("--------info.mImgUrl------" + info.mImgUrl);
			mInfos.add(info);
		}

		mPagerIndicator
				.initIndicator(0, mInfos.size(),
						R.drawable.indicator_dot_normal,
						R.drawable.indicator_dot_focus);

		BannerAdapter mBannerAdapter = new BannerAdapter(HomepageActivity.this,
				mInfos);
		mPagerAdapter = new LoopViewPagerAdapter(mBannerAdapter);
		mViewPager.setInfinateAdapter(mPagerAdapter);
		// 添加点击事件的监听

		mViewPager.setOnPageChangeListener(new BannerPageChangeListener(mInfos
				.size()));
		mViewPager.cancelScroll();
		if (mInfos.size() > 1) {
			mViewPager.startScroll();
		} else {
			mPagerIndicator.setVisibility(View.INVISIBLE);
		}

	}

	/**
	 * 伪数据
	 */
	private void setFalseDataGrad() {

		for (int i = 0; i < 7; i++) {
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("imageUrl",
					"http://dh2.kimg.cn/static/images/public/20150812/bdd8b0d092ff2479d7453ad685c6005c.jpg");
			map.put("name", "衣服名字");
			map.put("sale", "卖了几件");
			map.put("price", "价格");
			map.put("categoryId", (i + 1) + "");
			data_grid.add(map);
		}
		mAdapter_grid.update(data_grid);
	}

	/**
	 * api
	 * */
	private void loadingHomePage() {
		// TODO Auto-generated method stub

	}

	private class BannerPageChangeListener implements OnPageChangeListener {
		//
		public static final int SCROLL_STATE_IDLE = 0;
		//
		public static final int SCROLL_STATE_DRAGGING = 1;
		//
		public static final int SCROLL_STATE_SETTLING = 2;

		private int mSize;

		public BannerPageChangeListener(int size) {
			this.mSize = size;
		}

		@Override
		public void onPageScrollStateChanged(int state) {
			switch (state) {
			case SCROLL_STATE_IDLE:
				break;

			case SCROLL_STATE_DRAGGING:
				break;

			case SCROLL_STATE_SETTLING:
				break;
			}
		}

		@Override
		public void onPageScrolled(int position, float positionOffset,
				int positionOffsetPixels) {
		}

		@Override
		public void onPageSelected(int position) {
			int index = position % mSize;
			mPagerIndicator.refreshIndicator(index);

		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK
				&& event.getAction() == KeyEvent.ACTION_DOWN) {
			if (drawerLayout.isDrawerOpen(left_menu_layout)) {
				drawerLayout.closeDrawer(left_menu_layout);
			} else if ((System.currentTimeMillis() - exitTime) > 2000) {
				DialogUtils.showToast(this, "再按一次退出程序");
				exitTime = System.currentTimeMillis();

			} else {
				finish();
				System.exit(0);
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		// 左
		case R.id.person_center:
			drawerLayout.openDrawer(left_menu_layout);
			break;
		// 右
		case R.id.shopping:
			startActivity(new Intent(context, ShoppingCart.class));
			break;
		// 我
		case R.id.myself:
			startActivity(new Intent(context, InformationActivity.class));
			break;
		// 收藏夹
		case R.id.favorites:
			drawerLayout.closeDrawers();
			startActivity(new Intent(context, FavoriteActivity.class));
			break;

		// 未收货
		case R.id.not_receiving:
			drawerLayout.closeDrawer(Gravity.LEFT);
			startActivity(new Intent(context, UnReceiveActivity.class));
			break;

		// 未收货
		case R.id.had_receiving:
			drawerLayout.closeDrawer(Gravity.LEFT);
			startActivity(new Intent(context, ReceivedActivity.class));
			break;

		// 全部订单
		case R.id.all_orders:
			drawerLayout.closeDrawer(Gravity.LEFT);
			startActivity(new Intent(context, AllOrderActivity.class));
			break;
		// 邀请
		case R.id.invitation:
			drawerLayout.closeDrawer(Gravity.LEFT);
			startActivity(new Intent(context, InviteFriendActivity.class));
			break;
		// 设置
		case R.id.setting:
			drawerLayout.closeDrawer(Gravity.LEFT);
			startActivity(new Intent(context, SettingActivity.class));
			break;
		// 消息
		case R.id.message:
			drawerLayout.closeDrawer(Gravity.LEFT);
			startActivity(new Intent(context, MsgActivity.class));
			break;
		case R.id.exit:
			finish();
			System.exit(0);
			break;

		default:
			break;
		}

	}

}
